<?php
/**
物业端报修
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;


class RepairsController extends Controller{

    //安卓
    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址

    //ios
    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址



    public function pubsh($id){
        $select   = DB::table('repair')
            ->leftjoin('user','repair.user_repair','=','user.user_id')
            ->where('repair_id',$id)
            ->first();
        try {
            $push = new push($this->app_key, $this->master_secret, $this->url);
            $push->send_pub($select->phone, '您提的报事报修信息，我们已派人去处理', '5');
            $iospush = new push($this->appios_key, $this->masterios_secret, $this->urlios);
            $iospush->send_pub($select->phone, '您提的报事报修信息，我们已派人去处理', '5');
        }catch (\Exception $exception){
            exit;
        }

    }

    //全部报修数据
    public function AllRepairs(Request $request,$type,$types){
        if($request ->isMethod('post')){
            $village_id             = $request ->input('village_id');
            if($types ==4) {
                $data = DB::table('user')
                    ->select('repair_id', 'status', 'depict', 'repair_create_time', 'repair_image','repair_update_time')
                    ->leftjoin('repair', 'user.user_id', '=', 'repair.user_repair')
                    ->where('repair_type', $type)
                    ->where('user_village', $village_id)
                    ->where('status',2)
                    ->orderby('repair_create_time','ASE')
                    ->get();
                foreach ($data as $value) {
                        $status = $value->status;
                        $repair_id = $value->repair_id;
                        $depict = $value->depict;
                    if(empty($value ->repair_image)){
                        $repair_image = [];
                    }else {
                        $repair_image = explode(',', $value->repair_image);
                    }
                        $time = $value->repair_create_time;
                    $da[] = array('repair_id' => $repair_id,
                        'status' => $status,
                        'repair_image' => $repair_image,
                        'depict' => $depict,
                        'time' => $time,
                        'update_time' =>$value ->repair_update_time
                    );
                }
                if ($data == true) {
                    return json::json('success', '获取报修报事数据成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }else{
                $data = DB::table('user')
                    ->select('repair_id', 'status', 'depict', 'repair_create_time', 'repair_image','repair_update_time')
                    ->leftjoin('repair', 'user.user_id', '=', 'repair.user_repair')
                    ->where('repair_type', $type)
                    ->where('status',$types)
                    ->where('user_village', $village_id)
                    ->orderby('repair_create_time','ASE')
                    ->get();
                foreach ($data as $value) {

                        $status = $value->status;

                        $repair_id = $value->repair_id;

                        $depict = $value->depict;

                    if(empty($value ->repair_image)){
                        $repair_image = [];
                    }else {
                        $repair_image = explode(',', $value->repair_image);
                    }
                        $time = $value->repair_create_time;

                    $da[] = array('repair_id' => $repair_id, 'status' => $status, 'repair_image' => $repair_image, 'depict' => $depict, 'time' => $time,
                        'update_time' =>$value ->repair_update_time
                        );
                }
                if ($data == true) {
                    return json::json('success', '获取报修报事数据成功', $da);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }

        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function personnel($village_id){
        $data         = DB::table('hashslinger')
            ->select('hashslinger_id','name','hashslinger_phone','position','hashslinger_head')
            ->where('village_hashslinger',$village_id)
            ->get();
        foreach ($data as $value){
            $dd[]       =array(
                'hashslinger_id' =>$value->hashslinger_id,
                'name'            =>$value->name,
                'position'       =>$value->position,
            );
        }
        if(empty($dd)){
            return array();
        }else{
            return $dd;
        }


    }

    //报修详情
    public function SelectRepairs(Request $request){
        if($request ->isMethod('post')){
            $village_id     = $request ->input('village_id');
            $repair_id      = $request ->input('repair_id'); //报修报事ID
            $data           = DB::table('repair')
                            ->select('repair_type','repair_id','repair_image','depict','repair_create_time',
                                'province','city','district','village_name','phone','nickname','status','name','hashslinger_phone','quality','speed','content','wtbznr'
                                ,'floor','number','room','user_roomname')
                            ->leftjoin('user','repair.user_repair','=','user.user_id')
                            ->leftjoin('repair_type','repair.repair_id','=','repair_type.repairtype_repair')
                            ->leftjoin('village','user.user_village','=','village.village_id')
                            ->leftjoin('hashslinger','repair.repair_hashslinger','=','hashslinger.hashslinger_id')
                            ->where('repair_id',$repair_id)
                            ->get();
            $personnel      = $this ->personnel($village_id);

            foreach ($data as $value){
                    $repair_type = $value->repair_type;
                    $repair_id = $value->repair_id;

                if(empty($value ->repair_image)){
                    $repair_image = [];
                }else {
                    $repair_image = explode(',',$value->repair_image);
                }
                    $depict = $value->depict;
                    $time = $value->repair_create_time;
                    $village_name = $value->village_name;
                    $name         = $value ->name;
                    $user_roomname  = $value ->user_roomname;
                    $ha_phone        = $value ->hashslinger_phone;
                    $address      = $village_name.'-'.$user_roomname/*$value->floor.'-'.$value->number.'-'.$value ->room*/;
                    $phone = $value->phone;
                    $nickname = $value->nickname;
                    $status = $value->status;
                    $speed  = $value ->speed;
                    $quality = $value ->quality;
                    $content = $value ->content;
                    $wtbznr  = $value ->wtbznr;
                $da      = array('repair_type'=>$repair_type,
                    'repair_id'=>$repair_id,
                    'repair_image'=>$repair_image,
                    'depict'=>$depict,
                    'time'=>$time,'address'=>$address,'phone'=>$phone,'nickname'=>$nickname,'status'=>$status,
                    'name' =>$name,'hashslinger_phone'=>$ha_phone,'speed'=>$speed,'quality' =>$quality,'content' =>$content,
                    'wtbznr' =>$wtbznr,
                    'list' =>$personnel
                );
            }
            if ($data == true) {
                return json::json('success', '获取数据详情成功', $da);
            } else {
                $arr  = (object)array();
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function updateRepairs(Request $request){
        if($request ->isMethod('post')){
            $da           = (object)array();
            $repair_id             = $request ->input('repair_id');
            $repair_hashslinger    = $request ->input('hashslinger_id');
            $data                  = DB::table('repair') ->where('repair_id',$repair_id)
                ->update(['repair_hashslinger'=>$repair_hashslinger,
                    'status'   =>1
                ]);

            if($data == true){
/*                $select   = DB::table('repair')
                         ->leftjoin('user','repair.user_repair','=','user.user_id')
                         ->where('repair_id',$repair_id)
                         ->first();
                $push  =  new push($this ->app_key,$this ->master_secret,$this->url);
                $push  ->send_pub($select ->phone,'您提的报事报修信息，我们已派人去处理','5');
                $iospush  = new push($this ->appios_key,$this ->masterios_secret,$this->urlios);
                $iospush  ->send_pub($select ->phone,'您提的报事报修信息，我们已派人去处理','5');*/

                echo json::json('success', '人员派送成功', $da);
                $this ->pubsh($repair_id);
            }else{
                return json::json('failure', '由于网络或者该人员已经被到别的报修点', $da);
            }
        }
    }

    public function wtbznr(Request $request){
        if($request ->isMethod('post')){
            $da           = (object)array();
            $repair_id         = $request ->input('repair_id');
            $wtbznr            = $request ->input('wtbznr');
            $update            = DB::table('repair')->where('repair_id',$repair_id)
                ->update([
                    'wtbznr'=>$wtbznr,
                    'repair_update_time' =>date('Y-m-d H:i:s')
                ]);
            if($update == true){
                return json::json('success', '备注信息提交成功', $da);
            }else{
                return json::json('failure', '对不起信息提交失败', $da);
            }
        }
    }



}